// excerpts from http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#pragma once

#include "thread/Thread.h"
#include "noncopyable.h"

#include <boost/smart_ptr/scoped_ptr.hpp>
#include <unistd.h>
#include <vector>

namespace learnmuduo
{
class Channel;
class Poller;

class EventLoop : public noncopyable { // 因为基类把拷贝构造和拷贝赋值运算符声明为 `deleted`，派生类复制时需要先复制基类子对象而被编译器禁止，因此通过继承即可禁止复制。
public:
  EventLoop();
  ~EventLoop();

  void loop();
  void quit();
  // internal use only
  void updateChannel(Channel* channel); // 内部接口：更新Channel

  void assertInLoopThread() {
    if (!isInLoopThread()) {
      abortNotInLoopThread();
    }
  }

  bool isInLoopThread() const { return threadId_ == muduo::CurrentThread::tid(); }

private:
  void abortNotInLoopThread();

  typedef std::vector<Channel*> ChannelList; // 活跃Channel列表

  bool looping_; /* atomic */
  bool quit_; /* atomic */
  const pid_t threadId_;
  boost::scoped_ptr<Poller> poller_; // 持有Poller对象
  ChannelList activeChannels_; // 当前活跃的Channel集合
};
} // namespace learnmuduo
